﻿using Device.Domain;
using Device.Infrstructre.Interface;
using Device.Read.API.Applcation.Command.CarriersCommand;
using Device.Read.API.Applcation.Command.EquipmentCommand;
using MediatR;
namespace Device.Read.API.Applcation.CommandHandlers.EquipmentCommandHandlers
{
    public class SelectEquipmentCommandHandlers : IRequestHandler<SelectEquipmentCommand,PageDTO<EquipmentModel>>
    {
        private readonly IEquipmentRepository _equipmentRepository;
        public SelectEquipmentCommandHandlers(IEquipmentRepository equipmentRepository)
        {
            _equipmentRepository = equipmentRepository;
        }
        

        public async Task<PageDTO<EquipmentModel>> Handle(SelectEquipmentCommand request, CancellationToken cancellationToken)
        {
            var list =( await _equipmentRepository.GetAllAsync()).Where(x=>x.IsDel==false);
            if(!string.IsNullOrEmpty(request.Name))
            {
                list = list.Where(x=>x.EquipmentName.Contains(request.Name )).ToList();
            }
            if (!string.IsNullOrEmpty(request.Code))
            {
                list = list.Where(x => x.EquipmentCode.Contains(request.Code)).ToList();
            }
            //if (!string.IsNullOrEmpty(request.Oid))
            //{
            //    list = list.Where(x => x.Office_Id == request.Oid).ToList();
            //}
            //if (request.Uid > 0)
            //{
            //    list = list.Where(x => x.User_Id == request.Uid).ToList();
            //}

            PageDTO<EquipmentModel> page = new PageDTO<EquipmentModel>();
            page.TotalCount = list.Count();
            page.PageCount = (int)Math.Ceiling(page.TotalCount * 1.0 / request.PageSize);
            page.list = list.OrderByDescending(x => x.EquipmentId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return page;

        }
    }
}
